package net.zjitc.service.Impl;

import net.zjitc.mapper.CartMapper;
import net.zjitc.pojo.Cart;
import net.zjitc.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/18 8:49
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    CartMapper cartMapper;
    @Override
    public List<Cart> findByuid(Integer id) {
        return cartMapper.findByuid(id);
    }

    @Override
    public int delete(Integer id) {
        return cartMapper.delete(id);
    }

    @Override
    public int add(Cart cart) {
        cart.setNumber(1);
        cart.setChecked(0);
//        查询当前用户购物车情况
        Cart current = cartMapper.findByCart_now(cart.getPid(),cart.getUid());
        if (current != null)
        {
            cart.setNumber(cart.getNumber()+current.getNumber());
            return cartMapper.updateNumber(cart);
        }
//        不存在该商品时
        cart.setCreate_time(new Date());
        return cartMapper.add(cart);
    }

    @Override
    public int updateNumber(Cart cart) {
        return cartMapper.updateNumber(cart);
    }
}
